package com.by.controller;

import com.by.bean.ResultBean;
import com.by.bean.User;
import com.by.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@CrossOrigin//解决跨域
public class UserController extends BaseController{
    @Autowired
    UserService userService;
    @PostMapping("/login")
    public ResultBean login(@RequestBody User user){
        User u=userService.query(user.getName());
        if(null==u){
            return fail("账号不存在");
        }
        //账号存在
        //将用户输入的密码进行加密与数据库密码做比较
        String tem= DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        //加密
        if(!tem.equals(u.getPassword())){
            return fail("密码不正确");
        }

        return success("登录成功");
    }
//    注册
    @PostMapping("/regist")
    public ResultBean regist(@RequestBody User user){
        int rs=userService.insert(user);
        if(rs==1){
            return success("注册成功");
        }else{
            return fail("注册失败");
        }
    }
    @GetMapping("/query")
    public ResultBean query(){
        List<User> list=userService.queryAll();
        return success("相应成功",list);
    }
}
